/* Gauge - Performance Measurement Framework for Mobile Platforms
 * Copyright (C) 2013, Juha Hytonen <juha.a.hytonen@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.gauge.GaugeAgent;

import java.util.ArrayList;
import java.util.List;

import org.gauge.MeasurementStorage;
import org.gauge.Probe;

import org.gauge.GaugeAgent.PeriodicProbeRunner;

/**
 * Auxiliary class for BenchmarkController. Handles probes and probe runners.
 * 
 * @see BenchmarkController
 * @see Probe
 * @see ProbeController
 */
public final class ProbeController {
	
    private List<PeriodicProbeRunner> probeRunners;
    
    public ProbeController() {
    	probeRunners = new ArrayList<PeriodicProbeRunner>();
    }
    
    public void StopAllProbes() throws Exception {
    	for(PeriodicProbeRunner probeRunner : probeRunners) {
    		probeRunner.Stop();
    	}
    	probeRunners.clear();
    }

	public void ReadProbe(Class<? extends Probe> probeClass) throws Exception {
		try {
			Probe probe = createProbeFor(probeClass);
			MeasurementStorage.Add(probe.GetMeasurement());
		}
		catch(Exception e) {
			throw e;
		}
	}

	public void ReadProbeEvery(Class<? extends Probe> probeClass, int millisecs) throws Exception {
		try {
			checkIfProbeIsAlreadyStarted(probeClass);
			Probe probe = createProbeFor(probeClass);
			PeriodicProbeRunner probeRunner = createProbeRunnerFor(probe, millisecs);
			probeRunner.Start();
		}
		catch(Exception e) {
			throw e;
		}
	}
	
	private void checkIfProbeIsAlreadyStarted(Class<? extends Probe> probeClass) throws Exception {
		try {
			getRunnerForProbe(probeClass);
			throw new Exception("Probe is already started");
		}
		catch(Exception e) {
		}
	}
	
	private PeriodicProbeRunner getRunnerForProbe(Class<? extends Probe> probeClass) throws Exception {
		String probeClassName = probeClass.getName();
		for(PeriodicProbeRunner probeRunner : probeRunners) {
			String name = probeRunner.GetProbeClassName();
			if (name.equals(probeClassName)) {
				return probeRunner;
			}
		}
		throw new Exception("No such probe");
	}
	
	private Probe createProbeFor(Class<? extends Probe> probeClass) throws Exception {
		try {
			return probeClass.newInstance();
		}
		catch(Exception e) { // IllegalAccessException, InstantiationException
			throw new Exception("Failed to create probe");
		}
	}
	
	private PeriodicProbeRunner createProbeRunnerFor(Probe probe, int millisecsBetweenRuns) {
		PeriodicProbeRunner runner = new PeriodicProbeRunner(probe, millisecsBetweenRuns);
		probeRunners.add(runner);
		return runner;
	}

}
